package com.nanfang51g3.ckz.ui.update.info;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.view.View;
import android.widget.TextView;

import com.nanfang51g3.ckz.R;
import com.nanfang51g3.ckz.application.CkzApplication;
import com.nanfang51g3.ckz.event.InfoUpEvent;
import com.nanfang51g3.ckz.event.RxBus;
import com.nanfang51g3.ckz.ui.base.IPresenter;
import com.nanfang51g3.ckz.ui.base.Base2Activity;
import com.nanfang51g3.ckz.utils.AppManager;
import com.nanfang51g3.ckz.utils.ButtonUtils;
import com.nanfang51g3.ckz.utils.EasyToast;
import com.nanfang51g3.ckz.utils.KLog;
import com.nanfang51g3.ckz.utils.SPUtil;
import com.nanfang51g3.ckz.widget.ClearEditText;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/4/19.
 */
public class InfoUpActivity extends Base2Activity implements InfoUpContract.View {
    public static final int TYPE_01 = 1;
    public static final int TYPE_02 = 2;
    public static final int TYPE_03 = 3;
    InfoUpContract.InfoUpActionsListener personActionsListener;
    @Bind(R.id.index_tv_right_title)
    TextView indexTvRightTitle;
    @Bind(R.id.cet_info_up_content)
    ClearEditText cetInfoUpContent;
    private int id_title = 0;
    private int id_type = -1;
    private String id_content;
    @Override
    public IPresenter getPresenter() {
        return null;
    }
    @Override
    public int getContentView() {
        return R.layout.info_up;
    }

    @Override
    public boolean isShowLeftIcon() {
        return true;
    }

    @Override
    public boolean isShowRightIcon() {
        return false;
    }

    @Override
    public int setLeftIcon() {
        return R.mipmap.ic_accounting_left;
    }

    @Override
    public int setRightIcon() {
        return 0;
    }

    @Override
    public int showTitle() {
        return id_title;
    }

    @Override
    public void onClickLeft(View v) {
        AppManager.getAppManager().finishActivity(this);
    }

    @Override
    public void onClickRight(View v) {


    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void onIntent() {
        id_title = getIntent().getIntExtra("id_title", 0);
        id_content = getIntent().getStringExtra("id_content");
        id_type = getIntent().getIntExtra("id_type", -1);
    }

    @Override
    public void finalView(int type,String value) {
        AppManager.getAppManager().finishActivity(this);
        //发送更新个人资料界面
        /**
         * @see com.nanfang51g3.ckz.ui.person.PersonActivity#onCreate(Bundle)
         */
        RxBus.getDefault().post(new InfoUpEvent(type, value));
    }

    @Override
    public void showMessage(String message) {
        EasyToast.showShort(this, message);
        cetInfoUpContent.setText("");
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        personActionsListener = DaggerInfoUpContractComponent
                .builder()
                .appComponent(CkzApplication.getAppComponent())
                .infoUpPresenterModule(new InfoUpPresenterModule(this))
                .build().getInfoUpPresenter();
        KLog.i("TAG", personActionsListener == null ? "personActionsListener==null" : "personActionsListener!=null");
        ButterKnife.bind(this);
        cetInfoUpContent.setHint(id_content);
        indexTvRightTitle.setVisibility(View.VISIBLE);
        indexTvRightTitle.setText(getResources().getString(R.string.str_save));
        switch (id_type) {
            case TYPE_01:
                cetInfoUpContent.setInputType(InputType.TYPE_CLASS_TEXT);
                cetInfoUpContent.setHint(R.string.str_type_01);
                break;
            case TYPE_02:
                cetInfoUpContent.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
                cetInfoUpContent.setHint(R.string.str_type_02);
                break;
            case TYPE_03:
                cetInfoUpContent.setInputType(InputType.TYPE_CLASS_NUMBER);
                cetInfoUpContent.setHint(R.string.str_type_03);
                break;
        }
    }

    @OnClick(R.id.index_tv_right_title)
    public void index_tv_right_title(View v) {
        if (!ButtonUtils.isFastDoubleClick(R.id.index_tv_right_title)) {
            //写你相关操作即可

            final String userid = (String) SPUtil.get(this, "userid", "");
            String content = cetInfoUpContent.getText().toString().trim();

            switch (id_type) {
                case TYPE_01:
                    /**
                     * @see InfoUpPresenter#updateUserName(String, String)
                     */
                    personActionsListener.updateUserName(userid, content);
                    break;
                case TYPE_02:
                    /**
                     * @see InfoUpPresenter#updateEmail(String, String)
                     */
                    personActionsListener.updateEmail(userid, content);
                    break;
                case TYPE_03:
                    /**
                     * @see InfoUpPresenter#updateQQ(String, String)
                     */
                    personActionsListener.updateQQ(userid, content);
                    break;

            }
        }
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);//解除绑定，官方文档只对fragment做了解绑
    }
}
